package com.hzsd.zengbanyi.biaodan_text.utils;


import hzst.android.entity.VersionInfo;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;

import org.json.JSONObject;

public class GetWebJsonData {
	
	public static VersionInfo getVersionInfo(String url,String uploadPath){
		VersionInfo info = new VersionInfo();
		try {
			JSONObject obj = new JSONObject(getJsonData(url));
			info.setDownloadUrl("http://"+uploadPath+"/"+obj.getString("downloadUrl"));
			info.setMustUpdate(obj.getString("mustUpdate"));
			info.setVersionNum(Integer.valueOf(obj.getString("versionNum")));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return info;
	}
	
	public static String getJsonData(String urlPath) throws Exception {  
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
		byte[] data = new byte[1024];  

		int len = 0;  

		URL url = new URL(urlPath);  
		HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

		InputStream inStream = null;
		try {
			inStream = conn.getInputStream();
		} catch (Exception e) {
			e.printStackTrace();
	        if(e instanceof SocketTimeoutException  || e instanceof UnknownHostException){
	        	
	        }
			return "";
		}  
		while ((len = inStream.read(data)) != -1) {
			outStream.write(data, 0, len);
		}
		inStream.close();  

		String str = new String(outStream.toByteArray(), "UTF-8");

		return str;
	}

}
